# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: 665_非递减数列.py
@time: 2021/02/07

https://leetcode-cn.com/problems/non-decreasing-array/

"""


class Leetcode:

    def checkPossibility(self, nums: list) -> bool:
        ...
        count = 0
        for i in range(len(nums) - 1):
            ...
            if nums[i] > nums[i + 1]:
                ...
                count += 1
                if count > 1:
                    return False
                tmp = nums[i]
                nums[i] = nums[i + 1]
                before = self.change_value(nums)
                nums[i] = tmp
                nums[i + 1] = tmp
                after = self.change_value(nums)
                if not before and not after:
                    return False
                else:
                    return True
        return True

    def change_value(self, nums: list) -> bool:
        ...
        for i in range(len(nums) - 1):
            ...
            if nums[i] > nums[i + 1]:
                return False
        return True
        # [3,4,2,3] ,[3,2,2,3], [3,4,4,3]
        # [5,7,1,8]
        # [4,2,3]


if __name__ == '__main__':
    pass
    leetcode = Leetcode()
    # print(leetcode.checkPossibility([3, 4, 2, 3]))
    print(leetcode.checkPossibility([5, 7, 1, 8]))
